﻿using _537AppStore_API.Data;
using _537AppStore_API.Model;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace _537AppStore_API.Controller
{
    [Microsoft.AspNetCore.Mvc.Route("api/[controller]")]
    [ApiController]
    public class WebsiteController: ControllerBase
    {
        public readonly AppDbContext _db;
        public WebsiteController(AppDbContext db)
        {
            _db = db;
        }
        /// <summary>
        /// 获取所有网站
        /// </summary>
        [HttpGet]
        public async Task<ActionResult<IEnumerable<Website>>> GetWebsites()
        {
            return await _db.websites.ToListAsync();
        }
        /// <summary>
        /// 获取指定网站
        /// </summary>
        ///根据名称获取
        ///
        [HttpGet("{Name}")]
        public async Task<ActionResult<Website>> GetWebsite(string Name)
        {
            var website = await _db.websites.FindAsync(Name);
            if (website == null)
            {
                return NotFound();
            }
            return website;
        }

        /// <summary>
        /// 创建网站
        /// </summary>
        /// <param name="website"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public async Task<ActionResult<Website>> PostWebsite(Website website)
        {
            website.id = Guid.NewGuid();
            website.CreateTime = DateTime.Now;
            _db.websites.Add(website);
            await _db.SaveChangesAsync();

            // 返回 201 Created 及新创建的资源地址
            return CreatedAtAction(nameof(GetWebsite), new { Name = website.Name }, website);
        }
        /// <summary>
        /// 更新网站
        /// </summary>
        /// <param name="Name"></param>
        /// 
        [HttpPut("{name}")]
        public async Task<IActionResult> PutWebsite(string Name, Website website)
        {
            if (Name != website.Name)
            {
                return BadRequest();
            }
            _db.Entry(website).State = EntityState.Modified;
            website.CreateTime = DateTime.Now;
            try
            {
                await _db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!WebsiteExists(Name))
                {
                    return NotFound();
                }
                throw;
            }
            return NoContent();// 204 无内容
        }

        private bool WebsiteExists(string name)
        {
            return _db.websites.Any(e => e.Name == name);
        }
        /// <summary>
        /// 删除网站
        /// </summary>
        /// 
        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteWebsite(Guid id)
        {
            var website = await _db.websites.FindAsync(id);
            if (website == null)
            {
                return NotFound();
            }
            _db.websites.Remove(website);
            await _db.SaveChangesAsync();
            return NoContent();
        }
    }
}
